#!/bin/sh

cleanup () {
  unset -f nvm_ls_current nvm_alias_path nvm_has_colors nvm_alias nvm_print_alias_path nvm_print_default_alias
  case "${TEST_DIR-}" in
    /*) rm -rf "${TEST_DIR}" ;;
  esac
  unset TEST_DIR ALIAS_DIR MOCK_ALIAS_DIR MOCK_COLORS_STATUS
}

die () { echo "$@" ; cleanup ; exit 1; }

: nvm.sh
\. ../../../nvm.sh

\. ../../common.sh

TEST_DIR="$(mktemp -d)" || die 'failed to create temp dir'
NVM_DIR="${TEST_DIR}"
ALIAS_DIR="${TEST_DIR}/alias dir"
MOCK_ALIAS_DIR="${ALIAS_DIR}"
MOCK_COLORS_STATUS=0

unset NVM_LTS NVM_CURRENT
NVM_NO_COLORS='--no-colors'
NVM_HAS_COLORS='outer'

nvm_ls_current() {
  nvm_echo 'v9.9.9'
}

nvm_alias_path() {
  nvm_echo "${MOCK_ALIAS_DIR}"
}

nvm_has_colors() {
  return "${MOCK_COLORS_STATUS}"
}

nvm_alias() {
  nvm_echo "nvm_alias:${1-}"
  return 7
}

# the first field is the alias file's contents, so that glob order and sort order differ
nvm_print_alias_path() {
  [ "_${1-}" = "_${MOCK_ALIAS_DIR}" ] || nvm_echo "wrong alias dir >${1-}<"
  [ -f "${2-}" ] || return 0
  local CONTENTS
  CONTENTS="$(command cat "${2}")"
  [ "${CONTENTS}" != 'EMPTY' ] || return 0
  nvm_echo "${CONTENTS} ${2#"${1}/"} lts=${NVM_LTS-unset} colors=${NVM_HAS_COLORS-unset} current=${NVM_CURRENT-unset} nocolors=${NVM_NO_COLORS-unset}"
}

nvm_print_default_alias() {
  nvm_echo "default ${1-} lts=${NVM_LTS-unset} colors=${NVM_HAS_COLORS-unset} current=${NVM_CURRENT-unset} nocolors=${NVM_NO_COLORS-unset}"
}

mkdir -p "${ALIAS_DIR}/lts" || die 'failed to create alias dir'
echo 'zzz' > "${ALIAS_DIR}/aaa" || die 'failed to create alias'
echo 'yyy' > "${ALIAS_DIR}/bbb" || die 'failed to create alias'
echo 'xxx' > "${ALIAS_DIR}/a b1" || die 'failed to create alias'
echo 'www' > "${ALIAS_DIR}/stable" || die 'failed to create alias'
echo 'lts-a' > "${ALIAS_DIR}/lts/iron" || die 'failed to create alias'
echo 'lts-b' > "${ALIAS_DIR}/lts/hydrogen" || die 'failed to create alias'
echo 'EMPTY' > "${ALIAS_DIR}/lts/empty" || die 'failed to create alias'

S='colors=1 current=v9.9.9 nocolors=--no-colors'

# no argument: user aliases, then implicit aliases without a file, then LTS aliases, each section sorted
try nvm_list_aliases
EXPECTED="www stable lts=unset ${S}
xxx a b1 lts=unset ${S}
yyy bbb lts=unset ${S}
zzz aaa lts=unset ${S}
default iojs lts=unset ${S}
default node lts=unset ${S}
default unstable lts=unset ${S}
lts-a lts/iron lts=true ${S}
lts-b lts/hydrogen lts=true ${S}"
[ "_${CAPTURED_STDOUT}" = "_${EXPECTED}" ] || die "no argument: got >${CAPTURED_STDOUT}<, expected >${EXPECTED}<"
[ "_${CAPTURED_EXIT_CODE}" = '_0' ] || die "no argument: got exit code >${CAPTURED_EXIT_CODE}<, expected >0<"

try_err nvm_list_aliases
[ "_${CAPTURED_STDERR}" = '_' ] || die "no argument: got stderr >${CAPTURED_STDERR}<, expected none"

# a prefix filters user aliases, and implicit aliases need an exact match
try nvm_list_aliases a
EXPECTED="xxx a b1 lts=unset ${S}
zzz aaa lts=unset ${S}"
[ "_${CAPTURED_STDOUT}" = "_${EXPECTED}" ] || die "'a': got >${CAPTURED_STDOUT}<, expected >${EXPECTED}<"
[ "_${CAPTURED_EXIT_CODE}" = '_0' ] || die "'a': got exit code >${CAPTURED_EXIT_CODE}<, expected >0<"

try nvm_list_aliases 'a b'
EXPECTED="xxx a b1 lts=unset ${S}"
[ "_${CAPTURED_STDOUT}" = "_${EXPECTED}" ] || die "'a b': got >${CAPTURED_STDOUT}<, expected >${EXPECTED}<"

try nvm_list_aliases node
EXPECTED="default node lts=unset ${S}"
[ "_${CAPTURED_STDOUT}" = "_${EXPECTED}" ] || die "'node': got >${CAPTURED_STDOUT}<, expected >${EXPECTED}<"

try nvm_list_aliases unstable
EXPECTED="default unstable lts=unset ${S}"
[ "_${CAPTURED_STDOUT}" = "_${EXPECTED}" ] || die "'unstable': got >${CAPTURED_STDOUT}<, expected >${EXPECTED}<"

try nvm_list_aliases io
[ "_${CAPTURED_STDOUT}" = '_' ] || die "'io': got >${CAPTURED_STDOUT}<, expected nothing"

# an alias file shadows the implicit alias of the same name
try nvm_list_aliases stable
EXPECTED="www stable lts=unset ${S}"
[ "_${CAPTURED_STDOUT}" = "_${EXPECTED}" ] || die "'stable': got >${CAPTURED_STDOUT}<, expected >${EXPECTED}<"

# a prefix also filters LTS aliases
try nvm_list_aliases h
EXPECTED="lts-b lts/hydrogen lts=true ${S}"
[ "_${CAPTURED_STDOUT}" = "_${EXPECTED}" ] || die "'h': got >${CAPTURED_STDOUT}<, expected >${EXPECTED}<"

# LTS aliases match by prefix, not substring
try nvm_list_aliases ydro
[ "_${CAPTURED_STDOUT}" = '_' ] || die "'ydro': got >${CAPTURED_STDOUT}<, expected nothing"

try nvm_list_aliases nomatch
[ "_${CAPTURED_STDOUT}" = '_' ] || die "'nomatch': got >${CAPTURED_STDOUT}<, expected nothing"
[ "_${CAPTURED_EXIT_CODE}" = '_0' ] || die "'nomatch': got exit code >${CAPTURED_EXIT_CODE}<, expected >0<"

try_err nvm_list_aliases nomatch
[ "_${CAPTURED_STDERR}" = '_' ] || die "'nomatch': got stderr >${CAPTURED_STDERR}<, expected none"

# glob characters in the argument are literal
try nvm_list_aliases '*'
[ "_${CAPTURED_STDOUT}" = '_' ] || die "'*': got >${CAPTURED_STDOUT}<, expected nothing"

# `lts` without a slash is not delegated to nvm_alias
try nvm_list_aliases lts
[ "_${CAPTURED_STDOUT}" = '_' ] || die "'lts': got >${CAPTURED_STDOUT}<, expected nothing"

# `lts/` names are delegated to nvm_alias, with its output and exit code
try nvm_list_aliases lts/iron
[ "_${CAPTURED_STDOUT}" = '_nvm_alias:lts/iron' ] || die "'lts/iron': got >${CAPTURED_STDOUT}<, expected >nvm_alias:lts/iron<"
[ "_${CAPTURED_EXIT_CODE}" = '_7' ] || die "'lts/iron': got exit code >${CAPTURED_EXIT_CODE}<, expected >7<"

# only names starting with `lts/` are delegated
try nvm_list_aliases xlts/foo
[ "_${CAPTURED_STDOUT}" = '_' ] || die "'xlts/foo': got >${CAPTURED_STDOUT}<, expected nothing"
[ "_${CAPTURED_EXIT_CODE}" = '_0' ] || die "'xlts/foo': got exit code >${CAPTURED_EXIT_CODE}<, expected >0<"

try nvm_list_aliases 'lts/*'
[ "_${CAPTURED_STDOUT}" = '_nvm_alias:lts/*' ] || die "'lts/*': got >${CAPTURED_STDOUT}<, expected >nvm_alias:lts/*<"
[ "_${CAPTURED_EXIT_CODE}" = '_7' ] || die "'lts/*': got exit code >${CAPTURED_EXIT_CODE}<, expected >7<"

# without colors, NVM_HAS_COLORS is 0
MOCK_COLORS_STATUS=1
try nvm_list_aliases a
EXPECTED='xxx a b1 lts=unset colors=0 current=v9.9.9 nocolors=--no-colors
zzz aaa lts=unset colors=0 current=v9.9.9 nocolors=--no-colors'
MOCK_COLORS_STATUS=0
[ "_${CAPTURED_STDOUT}" = "_${EXPECTED}" ] || die "no colors: got >${CAPTURED_STDOUT}<, expected >${EXPECTED}<"

# called directly, its locals do not leak into the caller
nvm_list_aliases a > /dev/null
[ "_${NVM_HAS_COLORS-unset}" = '_outer' ] || die "NVM_HAS_COLORS leaked: got >${NVM_HAS_COLORS-unset}<, expected >outer<"
[ "_${NVM_CURRENT-unset}" = '_unset' ] || die "NVM_CURRENT leaked: got >${NVM_CURRENT-unset}<, expected >unset<"

# the `lts` subdirectory is created, even when delegating to nvm_alias
MOCK_ALIAS_DIR="${TEST_DIR}/new dir"
mkdir "${MOCK_ALIAS_DIR}" || die 'failed to create alias dir'
try nvm_list_aliases
[ -d "${MOCK_ALIAS_DIR}/lts" ] || die 'the lts alias dir was not created'
EXPECTED="default iojs lts=unset ${S}
default node lts=unset ${S}
default stable lts=unset ${S}
default unstable lts=unset ${S}"
[ "_${CAPTURED_STDOUT}" = "_${EXPECTED}" ] || die "empty alias dir: got >${CAPTURED_STDOUT}<, expected >${EXPECTED}<"

try nvm_list_aliases stable
EXPECTED="default stable lts=unset ${S}"
[ "_${CAPTURED_STDOUT}" = "_${EXPECTED}" ] || die "'stable' without a file: got >${CAPTURED_STDOUT}<, expected >${EXPECTED}<"

MOCK_ALIAS_DIR="${TEST_DIR}/newer dir"
mkdir "${MOCK_ALIAS_DIR}" || die 'failed to create alias dir'
nvm_list_aliases lts/iron > /dev/null
[ -d "${MOCK_ALIAS_DIR}/lts" ] || die 'the lts alias dir was not created when delegating'

cleanup
